package com.project.frba.bi.loading;

import org.springframework.context.ApplicationContext;

import com.project.frba.bi.BiUtil;
import com.project.frba.bi.queueing.OlapDAO;
import com.project.frba.bi.transformation.DimensionTable;
import com.project.frba.bi.transformation.TransformationResult;

public class LoaderImpl implements Loader{
	
	private OlapDAO dao;
	
	public LoaderImpl(){
		ApplicationContext context = BiUtil.getApplicationContext();
		dao = (OlapDAO)context.getBean("olapDao");
	}

	public void load(TransformationResult result) {
		Long start = BiUtil.getActualSeconds();		
		dropTables(result);
		loadDimensionTables(result);
		loadFactTable(result);
		Long end = BiUtil.getActualSeconds();
		System.out.println("Duracion loading: " + (end-start) );
	}


	private void dropTables(TransformationResult result) {
		StringBuffer dropBuffer = new StringBuffer();
		FactTableBuilder factBuilder = new FactTableBuilder(result.getFactTable());
		dropBuffer.append(factBuilder.getDropTableStatement());
		for (DimensionTable dimensionTable : result.getDimensionTables()) {
			DimensionTableBuilder dimTableBuilder = new DimensionTableBuilder(dimensionTable.getDimension());
			dropBuffer.append(dimTableBuilder.getDropTableStatement());
		}
		
		String[] statements = dropBuffer.toString().split(";");
		for (int i = 0; i < statements.length; i++) {
			//System.out.println(statements[i]);
			dao.executeDDLStatement(statements[i] + ";");			
		}
	}

	private void loadFactTable(TransformationResult result) {
		FactTableBuilder factBuilder = new FactTableBuilder(result.getFactTable());
		factBuilder.buildCreateTableStatement();
		String createFactTableStatement = factBuilder.getCreateFactTableStatement();
		String[] statements = createFactTableStatement.split(";");
		
		for (int i = 0; i < statements.length; i++) {
			dao.executeDDLStatement(statements[i] + ";");			
		}
		
		Long startIndex = 1L;
		Long endIndex = 8000L;
		
		while(startIndex <= result.getFactTable().getRowsLength()) {
			String factTableInsertStatement = null;
			if(endIndex > result.getFactTable().getRowsLength()) {
				factTableInsertStatement = result.getFactTable().getInsertStatement(startIndex, new Long(result.getFactTable().getRowsLength()));
			} else {
				factTableInsertStatement = result.getFactTable().getInsertStatement(startIndex, endIndex);
			}
			dao.executeDDLStatement(factTableInsertStatement);
			startIndex = endIndex + 1;
			endIndex += endIndex;
		}
			
		
	}


	private void loadDimensionTables(TransformationResult result) {
		for (DimensionTable dimensionTable : result.getDimensionTables()) {
			DimensionTableBuilder dimTableBuilder = new DimensionTableBuilder(dimensionTable.getDimension());
			dimTableBuilder.buildStatement();
			String createDimensionTableStatement = dimTableBuilder.getCreateDimensionTableStatement();
			String[] statements = createDimensionTableStatement.split(";");
			
			for (int i = 0; i < statements.length; i++) {
				//System.out.println(statements[i]);
				dao.executeDDLStatement(statements[i] + ";");			
			}
			
			String dimensionInsertStatement = dimensionTable.getInsertStatement();
			//System.out.println(dimensionInsertStatement);
			dao.executeDDLStatement(dimensionInsertStatement);			
		}
	}
	
	
	
	
}
